<?php

include_once 'SPDO.class.php';
include_once 'chauffage.class.php';
include_once 'store.class.php';
include_once 'equipement.class.php';

include_once 'modele/except/domotiqueErreur.class.php';
include_once 'modele/except/temperatureChauffage.class.php';

include_once 'modele/except/domotiqueConfort.class.php';
include_once 'modele/except/temperatureExt.class.php';

class Piece
{
	protected $droit;
	protected $droitUtilisateur;
	protected $ID;
	protected $nom;
	protected $equipement;
	protected $temperatureExt;

	public function __construct($m_ID, $m_nom, $m_droit, $m_droitUtilisateur, $m_tempExt)
	{
		$this->ID = $m_ID;
		$this->nom = $m_nom;
		$this->droit = $m_droit;
		$this->droitUtilisateur = $m_droitUtilisateur;
		$this->temperatureExt = $m_tempExt;

		try
		{
			$recupChauff = SPDO::pdo()->prepare("SELECT * FROM Chauffage, Element WHERE Element.ID_Element=Chauffage.ID_Element AND Element.ID_Piece = :id");
			$recupChauff->execute(array('id' => $this->ID));
				
			$i = 0;
				
			while ($result = $recupChauff->fetch(PDO::FETCH_OBJ))
			{
				$this->equipement[$i] = new Chauffage($result->ID_Chauffage, $result->Etat, $result->Temperature, $result->TemperatureMax, $result->TemperatureMin, $result->ID_Element, $result->Description, $this->nom, $this->droit, $this->droitUtilisateur);
				$i++;
			}
				
			$recupChauff->closeCursor();
				
			$recupStore = SPDO::pdo()->prepare("SELECT * FROM Stores, Element WHERE Stores.ID_Element = Element.ID_Element AND Element.ID_Piece = :id");
			$recupStore->execute(array('id' => $this->ID));
				
			while ($result = $recupStore->fetch(PDO::FETCH_OBJ))
			{
				$this->equipement[$i] = new Store($result->ID_Store, $result->Position, $result->ID_Element, $result->Description, $this->nom, $this->droit, $this->droitUtilisateur);
				$i++;
			}
				
			$recupStore->closeCursor();
		}
		catch (PDOException $e)
		{
			die($e->getMessage());
		}
	}

	public function __get($nom)
	{
		if($nom == "nom")
		{
			return $this->nom;
		}
		else if($nom == "ID")
		{
			return $this->ID;
		}
		else if($nom == "equipement")
		{
			return $this->equipement;
		}
	}

	public function afficheEquipement($erreur = null)
	{
		if(isset($erreur[$this->ID]))
		{
			echo "<div id='" .$this->ID. "' class='erreur'>";
		}
		else
		{
			echo "<div id='" .$this->ID. "' class='nonerreur'>";
		}
		
		echo "<div class='nompiece'>" .$this->nom. "</div>";

		if (count($this->equipement) != 0)
		{
			foreach ($this->equipement AS $cle => $val)
			{
				$val->afficheEquipement();
			}
		}
		else
		{
			echo "Aucun équipement pour cette pièce";
		}

		echo "</div>";
	}
	
	public function afficheModifierEquipement()
	{
		echo "<div id=" .$this->ID. ">";
		echo "<div class='nompiece'>" .$this->nom. "</div>";
		
		if (count($this->equipement) != 0)
		{
			foreach ($this->equipement AS $cle => $val)
			{
				$val->afficheModifierEquipement();
				echo "<br/>";
			}
		}
		else
		{
			echo "Aucun équipement à modifier pour cette pièce";
		}
		
		echo "<hr/>
				<div class='ajoutEquipement'>
					<label for='typeEquipement'>Ajouter un équipement</label>
					<input type='checkbox' name='ajoutChauffage" .$this->ID. "' value='true'>Chauffage
					<input type='checkbox' name='ajoutStore" .$this->ID. "' value='true'>Store
				</div>";
		
		echo "<hr/>
				<div class='suppPiece'>
						<input type='checkbox' name='suppPiece" .$this->ID. "' value='true'>Supprimer cette piece
				</div>
		</div>";
	}

	public function majEquipement()
	{
		if(count($this->equipement) != 0)
		{
			foreach ($this->equipement AS $cle => $val)
			{
				try 
				{
					$val->majEquipement($this->temperatureExt);
				}
				catch (DomotiqueConfort $e)
				{
					$e->notification();
				}
				catch (DomotiqueErreur $e)
				{
					$_SESSION['erreur'][$this->ID] = 1;
					$e->notification();
				}
				
			}
		}
	}
	
	public function modifierPiece()
	{		
		if(isset($_POST['ajoutChauffage' .$this->ID]) && $_POST['ajoutChauffage' .$this->ID] == "true")
		{
			?>
				<h4>Chauffage pour <?php echo $this->nom; ?></h4>
				<input type="hidden" name="chauffage<?php echo $this->ID; ?>">
				<label for="temperatureMax<?php echo $this->ID; ?>">Temperature maximale</label>
				<input type="number" name="temperatureMax<?php echo $this->ID; ?>" pattern="[1-9][0-9]*" required="required">
				<label for="temperatureMin<?php echo $this->ID; ?>">Temperature minimale</label>
				<input type="number" name="temperatureMin<?php echo $this->ID; ?>" pattern="[1-9][0-9]*"  required="required">
				<label for="description<?php echo $this->ID; ?>">Description</label>
				<input type="text" name="description<?php echo $this->ID; ?>" required="required">
				<label for="droit">Niveau de privilége</label>
				<select name="droit">
					<option value="0">Adminitrateur</option>
					<option value="1">Privilége haut</option>
					<option value="2">Privilége bas</option>
				</select>
			<?php 
		}
		
		if(isset($_POST['ajoutStore' .$this->ID]) && $_POST['ajoutStore' .$this->ID] == "true")
		{
			?>
				<h4>Store pour <?php echo $this->nom; ?></h4>
				<input type="hidden" name="store<?php echo $this->ID; ?>">
				<label for="description<?php echo $this->ID; ?>">Description</label>
				<input type="text" name="description<?php echo $this->ID; ?>" required="required">
				<label for="droit">Niveau de privilége</label>
				<select name="droit">
					<option value="0">Adminitrateur</option>
					<option value="1">Privilége haut</option>
					<option value="2">Privilége bas</option>
				</select>
			<?php 
		}
	}
	
	public function ajouterEquipement()
	{
		if(isset($_POST['chauffage' .$this->ID]))
		{
			if($_POST["temperatureMax" .$this->ID] > $_POST["temperatureMin" .$this->ID])
			{
				Chauffage::ajoutEquipement($_POST["temperatureMax" .$this->ID], $_POST["temperatureMin" .$this->ID], $_POST["description" .$this->ID], $this->ID, $_POST["droit"]);
			}
			else
			{
				throw new TemperatureChauffage("La temperature maximale doit être supérieure à la temperature minimale");
			}				
		}
		
		if(isset($_POST['store' .$this->ID]))
		{
			Store::ajoutEquipement($_POST["description" .$this->ID], $this->ID, $_POST["droit"]);
		}
	}

	public function supprimerEquipement()
	{
		if (count($this->equipement) != 0)
		{
			foreach ($this->equipement AS $cle => $val)
			{
				$val->supprimerEquipement();
			}
		}
	}
}
